package org.itworks.biodev.gui.panels;

import java.awt.event.ActionEvent;
import java.sql.*;
import java.util.*;
import java.util.*;
import java.util.Date;

import javax.swing.*;
import javax.swing.event.*;

import org.itworks.biodev.gui.*;
import org.itworks.biodev.model.*;
import org.itworks.helpers.mvc.*;

public class MeetingsPanelControler extends AbstractControler {
	
	private MeetingsPanelModel model = null;
	
	public MeetingsPanelControler(MeetingsPanelModel model) {
		this.model = model;
	}

	public void viewLoaded() {
		try {
			this.model.setMeetingsList(MeetingTable.getInstance().findAll());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public MeetingsPanelModel getModel() {
		return this.model;
	}

	public void createNewMeetingButtonActionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		try {
			EditMeetingDialogView editDialog = new EditMeetingDialogView( new EditMeetingDialogControler( new EditMeetingDialogModel(), null));
			editDialog.setModal(true);
			editDialog.setVisible(true);
			
			if(editDialog.getIsOK()) {
				this.model.setMeetingsList(MeetingTable.getInstance().findAll());
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}

	public void buttonColumnMeetingsActionPerformed(ActionEvent actionEvent,
			int selectedIndex) {
		// TODO Auto-generated method stub
		try {
			Meeting meetingToEdit = this.model.getMeetings().elementAt(selectedIndex);
			if(meetingToEdit.isAfterCurrentDate()) {
				EditMeetingDialogView editDialog = new EditMeetingDialogView( new EditMeetingDialogControler( new EditMeetingDialogModel(), meetingToEdit));
				editDialog.setModal(true);
				editDialog.setVisible(true);
			
				if(editDialog.getIsOK()) {
					this.model.setMeetingsList(MeetingTable.getInstance().findAll());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteSelectedMeetingsButtonActionPerformed(
			ActionEvent actionEvent, Vector<Integer> selectedIndexes) {
		// TODO Auto-generated method stub
		if(selectedIndexes.size() > 0) {
			if(JOptionPane.showConfirmDialog(
					null,
					"Etes-vous sur de vouloir supprimer les reunions selectionnees ? (N.B. : seules les reunions qui n'ont pas encore eu lieu seront supprimees",
					"Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				// Suppression
				for(Integer i : selectedIndexes) {
					Meeting m;
					try {
						m = MeetingTable.getInstance().find(i);

						if(m.isAfterCurrentDate()) {
							MeetingTable.getInstance().delete(m);
						} else {
							System.out.println("Before");
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				try {
					this.model.setMeetingsList(MeetingTable.getInstance().findAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

	public void meetingsListTableValueChanged(ListSelectionEvent listSelectionEvent, Meeting meeting) {
		// TODO Auto-generated method stub
		this.model.setParticipantsList(meeting);

		
		
	}

	public void addNewParticipantButtonActionPerformed(ActionEvent actionEvent, Meeting m) {
		// TODO Auto-generated method stub
		if(m != null) {
			if(m.isAfterCurrentDate()) {
				EditParticipantToMeetingDialogView dialog = new EditParticipantToMeetingDialogView(
					new EditParticipantToMeetingDialogControler(
						new EditParticipantToMeetingDialogModel(),
						m,
						new Customer()
						
					)
				);
				
				dialog.setModal(true);
				dialog.setVisible(true);
				
				if(dialog.getIsOk()) {
					this.model.setParticipantsList(m);
				}
			} else {
				JOptionPane.showMessageDialog(null,
					"Impossible d'ajouter un participant à une réunion expirée !",
					"Erreur",
					JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}

	public void filterDateButtonActionPerformed(ActionEvent actionEvent,
			String beginDate, String endDate) {
		if(beginDate.equals("") && endDate.equals("")) {
			
			try {
				this.model.setMeetingsList(MeetingTable.getInstance().findAll());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			// TODO Auto-generated method stub
			StringTokenizer beginSlashToken	= new StringTokenizer(beginDate, "/");
			StringTokenizer endSlashToken	= new StringTokenizer(endDate, "/");
			if(beginSlashToken.countTokens() != 3 || endSlashToken.countTokens() != 3) {
				JOptionPane.showMessageDialog(null,
					"Date de début ou de fin invalide(s) (format : jj/mm/AAAA)", "Erreur",
					JOptionPane.ERROR_MESSAGE);
			} else {
				try {
					java.util.Date firstDate = new java.util.Date();
					Calendar c = Calendar.getInstance();
					c.setTime(firstDate);
					
					c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(beginSlashToken.nextToken()));
					c.set(Calendar.MONTH, Integer.parseInt(beginSlashToken.nextToken())-1);
					c.set(Calendar.YEAR, Integer.parseInt(beginSlashToken.nextToken()));
	
					firstDate = c.getTime();
	
					
					c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(endSlashToken.nextToken()));
					c.set(Calendar.MONTH, Integer.parseInt(endSlashToken.nextToken())-1);
					c.set(Calendar.YEAR, Integer.parseInt(endSlashToken.nextToken()));
	
					
					java.util.Date lastDate = new java.util.Date();
					lastDate = c.getTime();
					
				
					this.model.setMeetingsList(MeetingTable.getInstance().findAllByDateBetween(firstDate, lastDate));
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, "Les dates sont invalides (format requis : jj/mm/AAAA) !", "Erreur",
							JOptionPane.ERROR_MESSAGE);
				}
				
			}
			
		}
		
	}
}
